let requestUrl;

function addHandler() {
	let xingyinId = $("#xingyin_id").combobox("getValue");

	if(xingyinId) {
		$("#xingyinId").combobox("setValue", xingyinId);
	}

	requestUrl = "/xingyin_view_attribute/insert";

	$("#view_attribute_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#view_attribute_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/xingyin_view_attribute/updateById";

		$("#id").val(rowData.id);
		$("#type").combobox("setValue", rowData.type);
		$("#min").numberspinner("setValue", rowData.min);
		$("#max").numberspinner("setValue", rowData.max);
		$("#xingyinId").combobox("setValue", rowData.xingyinId);
		$("#propertyId").combobox("setValue", rowData.propertyId);

		$("#view_attribute_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#view_attribute_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/xingyin_view_attribute/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#view_attribute_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/property_type/selectAll",
        valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-星印属性类型-"
	});

	$("#property_id").combobox({
		url: "/property/selectAll",
        valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-属性类型-"
	});

	$("#xingyin_id").combobox({
		url: "/xingyin_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-星印类型-",
		groupField: "grade",
		groupFormatter: function (group) {
			return "<div class='green'>" + group + "级</div>";
		},
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let propertyId = $("#property_id").combobox("getValue");
		let xingyinId = $("#xingyin_id").combobox("getValue");
		let type = $("#_type_").combobox("getValue");

		$("#view_attribute_list").datagrid("load", {
			propertyId: propertyId,
			xingyinId: xingyinId,
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#type").combobox({
		url: "/property_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto"
	});

	$("#propertyId").combobox({
		url: "/property/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		editable: true,
		panelHeight: "auto"
	});

	$("#min").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#max").numberspinner({
		min: 0,
		width: 150,
		required: true
	});

	$("#xingyinId").combobox({
		url: "/xingyin_category/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		groupField: "grade",
		groupFormatter: function (group) {
			return "<div class='green'>" + group + "级</div>";
		},
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#view_attribute_dialog").dialog({
		title: "星印预览属性",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#view_attribute_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#view_attribute_dialog").dialog("close");
						$("#view_attribute_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#view_attribute_dialog").dialog("close");
				$("#view_attribute_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#view_attribute_list").datagrid({
		url: "/xingyin_view_attribute/selectByPage",
		method: "get",
		height: 640,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			// {field: "id", title: "编号", align: "center"},
			{field: "type", title: "星印属性类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/property_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#property_type_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='property_type_" + rowIndex + "'></div>";
				}
			},
			{field: "propertyId", title: "属性类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/property/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#property_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='property_" + rowIndex + "'></div>";
				}
			},
			{field: "min", title: "最小值", align: "center", width: 100},
			{field: "max", title: "最大值", align: "center", width: 100},
			{field: "xingyinId", title: "星印类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/xingyin_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#xingyin_" + rowIndex).attr("src", data.image)
							.attr("alt", data.name);
					}, error);

					return "<img height='" + size + "' id='xingyin_" + rowIndex + "' />";
				}
			}
		]]
	});

});